#ifndef COMMON_H_
#define COMMON_H_

#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <pthread.h>
#include <fcntl.h>

#define DEBUG 0
#define NOTICE 1
#define WARNING 2
#define FATAL 3

typedef int int32;
typedef short int16;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned long ulong;
typedef unsigned char uchar;
typedef struct sockaddr sockaddr_t;
typedef struct timeval timeval_t;

void _WriteLog(int LEVEL, char *file, int line, char *fmt, ...);

#define WriteLog(LEVEL, fmt, arg...) do { \
    _WriteLog(LEVEL, __FILE__, __LINE__, fmt, ## arg);\
} while (0)

extern std::string CONF_PATH;

const std::string APP_NAME = "Server";

extern volatile bool SAFE_STOP_FLAG;

int32 SocketRead(int32 fd, char* buf, int32 nbytes, int32 timeOut = 50);

int32 SocketWrite(int32 fd, const char *buf, int32 nbytes, int32 timeOut = 50);

std::string GetIP(struct sockaddr_in* addr);

int32 GetPort(struct sockaddr_in* addr);

std::string ltrim(std::string str, std::string rmv);

std::string rtrim(std::string str, std::string rmv);

std::string trim(std::string str, std::string rmv);

int32 Split(std::string str, std::string delim, std::vector<std::string>& parts);

timeval_t GetNowTimeVal();

double TimeDiff(timeval_t s, timeval_t e);

class Timer
{
   private:
        timeval_t s;
        timeval_t e;

    public:
        Timer(){};
        ~Timer(){};
        void Start();
        double Diff();//ms
};

#endif
